#include <iostream>
#include <cmath>
#include "kittrs232.h"
#include <string>
#include "gps.h"
#include <stdlib.h>




using namespace std;

GPS::GPS(int port) {
    this->port = port;//This number indicates the port number being used.  In this case port 17 represents tty/USB1, the second usb serial port.
    OpenComport(port,4800, C8n1);
}
void GPS::getCoords() {
    unsigned char buffer[256];
    char *output, *final;
    usleep(260000);
    PollComport(16, buffer,sizeof(buffer)-1);


    cout << buffer << endl;
    output = strstr(reinterpret_cast<char*>(buffer), "$GPGGA");
    char *token, *latitude, *longitude;
    token = strtok(output, ", ");

    for(int i=0; i < 5; i++) {
        switch(i) {
            case 2:
            {
                latitude = token;
                break;
            }
            case 4:
            {
                longitude = token;
                break;
            }
        }

        token = strtok(NULL, ", ");
    }
    cout << "Latitude: " << latitude << endl;
    cout << "Longitude: " << longitude << endl;
}

float GPS::getLat(){


    return atof(this->latChar);

}

float GPS::getLong(){

    return atof(this->lonChar);

}
